using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Nsb;
using jQueryApi;

namespace ThisILove
{
    public static class ListEntry
    {
        public static jQueryObject ListBox;
        public static jQueryObject ListBoxWrapper;
        public const string ThisILoveDataKey = "ThisILoveData";

        [PreserveName, PreserveCase]
        public static void Widget()
        {
            ListBox = jQuery.Select("#ListBox")
                ;
            ListBoxWrapper = ListBox.Closest(".Gig")
                .Hide()
                ;
        }

        public static void StartList()
        {
            ListBox.Html(NewList(WordList.Word));
            ListBoxWrapper.FadeIn(400,
                delegate
                {
                    ListEditor.EditText(ListBoxWrapper, WordList.Word);
                });
        }

        public static Dictionary GetData()
        {
            return (Dictionary)Storage.GetLocal(ThisILoveDataKey) ?? new Dictionary();
        }

        public static void SaveData()
        {
            Dictionary data = GetData();
            data[(new Date()).GetTime().ToString()] = ListEditor.EditFinish(ListBoxWrapper);
            Storage.SetLocal(ThisILoveDataKey, data);
        }

        public static void ShowData()
        {
            int t = ListBox.Position().Top;
            int h = ListBox.GetHeight();

            jQuery.Select(".AutoData").Remove();
            foreach (KeyValuePair<string, string> p in GetData())
            {
                jQueryObject nw = ListBox.Clone().InsertAfter(ListBox);
                nw.Find(".GigC").First().Html(p.Value);
                int d = t + h + 10;
                nw.CSS(new Dictionary("top", d));
                t += d;
                h = nw.GetHeight();
            }
        }

        public static string NewList(string word)
        {
            return String.Format("<h3>{0}</h3><ol></ol>", word);
        }
    }
}
